package com.vavr.checkincms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import com.alibaba.fastjson2.JSON;
import com.vavr.checkincms.common.domain.BaseResult;
import com.vavr.checkincms.common.domain.ResultEnum;
import com.vavr.checkincms.domain.bo.MeetingParticipantBO;
import com.vavr.checkincms.domain.dto.MeetingParticipantDTO;
import com.vavr.checkincms.service.MeetingParticipantService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会议参与人Controller
 *
 * @since 2024.08
 */
@RestController
public class MeetingParticipantController {
    /**
     * meetingParticipantService
     */
    private final MeetingParticipantService meetingParticipantService;

    public MeetingParticipantController(MeetingParticipantService meetingParticipantService) {
        this.meetingParticipantService = meetingParticipantService;
    }

    /**
     * 通过手机号码查询参会者信息
     *
     * @param bo {@link MeetingParticipantBO}
     * @return 参会者信息
     */
    @PostMapping("/getParticipantInfo")
    public BaseResult<MeetingParticipantDTO> getParticipantInfo(@RequestBody MeetingParticipantBO bo,
            HttpServletRequest request) {
        MeetingParticipantDTO meetingParticipant = meetingParticipantService.getMeetingParticipantByTel(bo, request);
        return BaseResult.of(ResultEnum.OK, meetingParticipant);
    }

    /**
     * 添加会议人员信息
     *
     * @param requestJson {@link MeetingParticipantBO}
     * @return 参会者信息
     */
    @PostMapping("/addParticipantInfo")
    public BaseResult<String> addParticipantInfo(@RequestBody String requestJson, HttpServletRequest request) {
        MeetingParticipantBO bo = JSON.parseObject(requestJson, MeetingParticipantBO.class);
        return BaseResult.of(ResultEnum.OK, meetingParticipantService.addMeetingParticipant(bo, request));
    }

    /**
     * 导出用户信息
     *
     * @param bo {@link MeetingParticipantBO}
     * @return 参会者信息
     */
    @PostMapping("/getAllUserInfo")
    public BaseResult<String> getAllUserInfo(@RequestBody MeetingParticipantBO bo, HttpServletResponse response,
            HttpServletRequest request) {
        meetingParticipantService.getAllUserInfo(bo, response, request);
        return BaseResult.of(ResultEnum.OK, ResultEnum.OK.getMsg());
    }

    /**
     * 导出用户信息
     *
     * @param code {@link String}
     * @param meetingId 会议id
     * @return 参会者信息
     */
    @GetMapping("/getAllUserInfo")
    public BaseResult<String> getAllUserInfo(@RequestParam("code") @NotBlank(message = "code不能为空") String code,
            @RequestParam("meetingId") String meetingId, HttpServletResponse response, HttpServletRequest request) {
        MeetingParticipantBO bo = new MeetingParticipantBO();
        if (Strings.isNotBlank(code)) {
            bo.setUserId(code);
            bo.setMeetingId(Strings.isNotBlank(meetingId) ? meetingId : "ldlpx2024");
        }
        meetingParticipantService.getAllUserInfo(bo, response, request);
        return BaseResult.of(ResultEnum.OK, ResultEnum.OK.getMsg());
    }
}
